import { NextRequest, NextResponse } from 'next/server'

export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params
    const projectId = id
    
    // 模拟分支数据
    const branches = [
      {
        _id: 'main',
        name: '主分支',
        isMain: true,
        isActive: true,
        createTime: '2024-01-01T00:00:00Z',
        creatorName: '系统管理员'
      },
      {
        _id: 'dev-001',
        name: '开发分支001',
        isMain: false,
        isActive: true,
        createTime: '2024-01-15T10:30:00Z',
        creatorName: '张三'
      },
      {
        _id: 'feature-002',
        name: '功能分支002',
        isMain: false,
        isActive: false,
        createTime: '2024-02-01T14:20:00Z',
        creatorName: '李四'
      }
    ]

    return NextResponse.json(branches)
  } catch (error) {
    console.error('获取项目分支失败:', error)
    return NextResponse.json(
      { error: '获取项目分支失败' },
      { status: 500 }
    )
  }
}

export async function POST(
  request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  try {
    const { id } = await params
    const projectId = id
    const body = await request.json()
    
    // 这里应该创建新分支的逻辑
    console.log('创建新分支:', { projectId, ...body })
    
    return NextResponse.json({ 
      message: '分支创建成功',
      branch: {
        _id: `branch-${Date.now()}`,
        name: body.name,
        isMain: false,
        isActive: true,
        createTime: new Date().toISOString(),
        creatorName: body.creatorName || '当前用户'
      }
    })
  } catch (error) {
    console.error('创建项目分支失败:', error)
    return NextResponse.json(
      { error: '创建项目分支失败' },
      { status: 500 }
    )
  }
}